package sockety.client.interf;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import java.awt.CardLayout;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import java.awt.SystemColor;
import javax.swing.JLabel;
import net.miginfocom.swing.MigLayout;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import static java.lang.System.out;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import java.awt.ComponentOrientation;


public class ClientInterface {
	
	private static final String menuItemConn = "Connection";
	private static final String menuItemSett = "Settings";
	private static String menuItemNames[] = {menuItemConn, menuItemSett};
	
	private JFrame frame;
	private JPanel panelContent;
	private JSplitPane spConnection;
	private JTextArea textArea;
	private JTextArea textArea_1;
	private JScrollPane scrpList;
	private JList list;
	private JPanel pSettings;
	private JLabel lblPort;
	private JLabel lblAddres;
	private JTextField tfAddress;
	private JTextField tfPort;
	private final JPanel pFooter = new JPanel();
	private CardLayout cl;
	private JButton btnNewButton;
	private final Action action = new SwingAction();
	private Client client;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClientInterface window = new ClientInterface();
					window.frame.setVisible(true);
					window.spConnection.setDividerLocation(window.panelContent.getSize().height /2);
					
					
					
					window.client = new Client();
					
					//System.out.println("Panel h " + window.panel.getSize().height );
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
		
		
	}
	/**
	 * Load config
	 */
	private  void loadConfiguration(){
		String [] config = App.loadConfig();
		tfAddress.setText(config[0]);
		tfPort.setText(config[1]);
	}

	/**
	 * Create the application.
	 */
	public ClientInterface() {
		initialize();
		loadConfiguration();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 536, 364);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new BorderLayout(0, 0));
		pFooter.setPreferredSize(new Dimension(10, 24));
		
		//panel.setMinimumSize(new Dimension(-1, 50));
		frame.getContentPane().add(pFooter, BorderLayout.SOUTH);
		
		btnNewButton = new JButton("Connect");
		btnNewButton.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		
		btnNewButton.setBounds(0, 0, 106, 23);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				if(client.isStarted()){
					client.stop();
					if(!client.isStarted()){
						btnNewButton.setText("Connect");
					}
				}
				else{
					String address = tfAddress.getText().trim();
					String port = tfPort.getText().trim();
					/*VALIDACJA*/
					client.start(address, port);
					if(client.isStarted()){
						btnNewButton.setText("Disconnect");
					}
				}
				out.println("connect");
			}
		});
		pFooter.setLayout(null);
		btnNewButton.setMinimumSize(new Dimension(91, 17));
		btnNewButton.setMaximumSize(new Dimension(91, 17));
		
		pFooter.add(btnNewButton);
		//btnNewButton.putClientProperty("JComponent.sizeVariant", "large");
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setDividerLocation(100);
		frame.getContentPane().add(splitPane);
		
		panelContent = new JPanel();
		splitPane.setRightComponent(panelContent);
		cl = new CardLayout(0,0);
		panelContent.setLayout(cl);
		
		
		System.out.println("Panel h " + panelContent.getSize().height );
		
		pSettings = new JPanel();
		pSettings.setName(menuItemSett);
		panelContent.add(pSettings, menuItemSett);
		pSettings.setLayout(new MigLayout("", "[46px][46px,grow]", "[14px][][][][][][][][][][]"));
		
		lblAddres = new JLabel("IP Address");
		pSettings.add(lblAddres, "cell 0 0,alignx trailing,aligny center");
		
		tfAddress = new JTextField();
		pSettings.add(tfAddress, "cell 1 0");
		tfAddress.setColumns(10);
		
		lblPort = new JLabel("Port");
		pSettings.add(lblPort, "cell 0 1,alignx trailing,aligny center");
		
		tfPort = new JTextField();
		pSettings.add(tfPort, "cell 1 1,aligny top");
		tfPort.setColumns(5);
		
		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				App.saveConfig(new String[]{
						tfAddress.getText().trim()
						,tfPort.getText().trim()
						});
			}
		});
		pSettings.add(btnSave, "cell 1 10");
		
		spConnection = new JSplitPane();
		spConnection.setName(menuItemConn);
		spConnection.setOrientation(JSplitPane.VERTICAL_SPLIT);
		spConnection.setDividerLocation(150);
		panelContent.add(spConnection, menuItemConn);
		
		
		textArea = new JTextArea();
		spConnection.setRightComponent(textArea);
		
		textArea_1 = new JTextArea();
		textArea_1.setBackground(SystemColor.inactiveCaptionBorder);
		textArea_1.setEditable(false);
		spConnection.setLeftComponent(textArea_1);
		
		scrpList = new JScrollPane();
		splitPane.setLeftComponent(scrpList);
		
		list = new JList(menuItemNames);
		list.addListSelectionListener(new ListSelectionListener() {
			
			public void valueChanged(ListSelectionEvent event) {
				if(!event.getValueIsAdjusting()){
					JList list = (JList)event.getSource();
					String s = (String)list.getSelectedValue();
					out.println(s);
					cl.show(panelContent, s);
				}
			}
		});
		scrpList.setViewportView(list);
		
	}
	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "SwingAction");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
		}
	}
}
